package com.itboye.ihomebank.base;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.support.multidex.MultiDexApplication;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Toast;

import com.alibaba.mobileim.YWAPI;
import com.alibaba.wxlib.util.SysUtil;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.baidu.location.service.LocationService;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.constants.SPContants;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;

public class MyApplcation extends MultiDexApplication {

	public static MyApplcation ctx;

	private Toast toast;

	private List<WeakReference<Activity>> activityList = new LinkedList<>();

	protected String afterOpen;

	protected String activity;

	protected String extra;

	protected org.json.JSONObject jsonObject2;

	protected String afterOpen2;
	String baseUrl = "http://sunsun.itboye.com/index.php/Webview/index/systemMessage?";

	protected String order_code;

	protected String id;

	protected int type;

	protected JSONObject jsonObject;
	boolean isReadOne, isReadTwo;

	public LocationService locationService;

	private static RequestQueue queue;

	// private List<WeakReference<Activity>> activitys = new
	// ArrayList<WeakReference<Activity>>();

	// private UncaughtExceptionHandler exceptionHandler;
	public static MyApplcation getInstance() {
		return ctx;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		locationService = new LocationService(getApplicationContext());
		ctx = this;
		queue = Volley.newRequestQueue(ctx);
		if (mustRunFirstInsideApplicationOnCreate()) {
			// todo 如果在":TCMSSevice"进程中，无需进行openIM和app业务的初始化，以节省内存
			return;
		}
		SysUtil.setApplication(this);
		// 初始化云旺SDK
		// InitHelper.initYWSDK(this);
		YWAPI.init(this, SPContants.YW_APPKEY);
		PushAgent mPushAgent = PushAgent.getInstance(this);
		// 注册推送服务，每次调用register方法都会回调该接口
		mPushAgent.register(new IUmengRegisterCallback() {

			@Override
			public void onSuccess(String deviceToken) {
				// 注册成功会返回device token
			}

			@Override
			public void onFailure(String s, String s1) {

			}
		});
		mPushAgent.setDebugMode(false);
		PushAgent.getInstance(this).onAppStart();
		// 初始化反馈功能(未使用反馈功能的用户无需调用该初始化)

		// YWAPI.initFeedBack(this);

	}

	private boolean mustRunFirstInsideApplicationOnCreate() {
		// 必须的初始化
		SysUtil.setApplication(this);
		return SysUtil.isTCMSServiceProcess(getApplicationContext());
	}

	public void showNetWorkError() {
		if (toast == null) {
			toast = Toast.makeText(MyApplcation.ctx, "", 1);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.setView(LayoutInflater.from(MyApplcation.ctx).inflate(
					R.layout.dialog_network_error, null));
		}

		toast.show();
	}

	public void addActivity(Activity activity) {
		activityList.add(new WeakReference<Activity>(activity));
	}

	// 遍历所有Activity并finish
	public void exit() {
		for (WeakReference<Activity> activity : activityList) {
			if (activity.get() != null) {
				activity.get().finish();
			}

		}
		System.exit(0);
	}

	/**
	 * @return the queue
	 */
	public static RequestQueue getQueue() {
		return queue;
	}

	public static <T> void addRequest(Request<T> request) {
		request.addMarker("itboye");
		getQueue().add(request);
	}
}
